package org.monster.context.war;

import org.monster.context.exception.FileException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * war
 *
 * @author Monster
 */
public class War implements Compresse {

    /**
     * 解压
     *
     * @param file 文件
     * @throws IOException   输入输出异常
     * @throws FileException 文件异常
     */
    public void unpack(File file) throws IOException, FileException {
        if (!file.exists()) {
            throw new FileException("[" + file.getName() + "] 文件不存在！");
        }
        ZipFile zipFile = new ZipFile(file);
        File parentFile = file.getParentFile().getAbsoluteFile();
        File warFolder = getWarFolder(parentFile,
                getWarName(file.getPath()));
        for (Enumeration<? extends ZipEntry> entries = zipFile.entries();
             entries.hasMoreElements(); ) {
            ZipEntry zipEntry = entries.nextElement();
            if (zipEntry.isDirectory()) {
                mkdirsWar(warFolder, zipEntry);
                continue;
            }
            writeEntry(warFolder, zipFile, zipEntry);
        }
        zipFile.close();
    }

    /**
     * 解压
     *
     * @param filePath 文件路径
     * @throws IOException   输入输出异常
     * @throws FileException 文件异常
     */
    public void unpack(String filePath) throws IOException, FileException {
        unpack(new File(filePath));
    }


    /**
     * 将压缩包里面的每一个条目，写入文件。
     *
     * @param parentFile 父文件
     * @param zipEntry   压缩目录
     * @throws IOException 输入输出异常
     */
    private void writeEntry(File parentFile, ZipFile zipFile, ZipEntry zipEntry)
            throws IOException {
        InputStream is = zipFile.getInputStream(zipEntry);
        byte[] b = new byte[(int) zipEntry.getSize()];
        int len;
        FileOutputStream fis = new FileOutputStream(
                getWarEntry(parentFile, zipEntry));
        while ((len = is.read(b)) != -1) {
            fis.write(len);
        }
        fis.close();
        is.close();
    }

    /**
     * 创建war文件夹
     *
     * @param warFolder war文件夹
     * @param zipEntry  压缩目录
     * @throws FileException 文件异常
     */
    private void mkdirsWar(File warFolder, ZipEntry zipEntry)
            throws FileException {
        File file = getWarEntry(warFolder, zipEntry);
        file.mkdirs();
    }

    /**
     * 获取war条目文件
     *
     * @param warFolder war文件夹
     * @param zipEntry  压缩目录
     * @return 文件
     */
    private File getWarEntry(File warFolder, ZipEntry zipEntry) {
        return new File(warFolder, zipEntry.getName());
    }

    /**
     * 获取war文件夹
     *
     * @param parentFile 父文件
     * @param simpleName 简单名称
     * @return 文件名
     */
    private File getWarFolder(File parentFile, String simpleName) {
        return new File(parentFile, simpleName);
    }

    /**
     * 获取war文件名
     *
     * @param warPath war路径
     * @return war文件名
     */
    public String getWarName(String warPath) {
        int lastIndex = warPath.lastIndexOf("\\");
        if (lastIndex == -1) {
            return warPath.substring(0, warPath.indexOf("."));
        }
        return warPath.substring(lastIndex, warPath.indexOf("."));
    }
}
